/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Tetravex;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;

/**
 *
 * @author alrz
 */
public class GameZone extends JPanel implements MouseListener, MouseMotionListener
{
    private int size;
    private boolean crearJuego;
    private ArrayList<Figure> ArrayFigure;
    private Figure figuraMovida;
    
    public GameZone()
    {
        super();
        super.setLayout(null);
        this.size = 3;
        this.crearJuego = true;
        this.setBackground(Color.green);
        this.ArrayFigure = new ArrayList<Figure>();
        this.addMouseListener(this);
        this.addMouseMotionListener(this);

    }
    
    @Override
    public void paint(Graphics g)
    {
        super.paintComponent(g);  
        g.setColor(Color.BLACK);
        g.drawRect(100, 100, 300, 300);
        g.drawLine(200, 100, 200, 400);
        g.drawLine(300, 100, 300, 400);
        g.drawLine(100, 200, 400, 200);
        g.drawLine(100, 300, 400, 300);
        if(crearJuego)
        {
            this.crearJuego = false;
            this.createZone();
        }
    }
    
    public void createZone()
    {
        Figure matriz[][] = new Figure[this.size][this.size];
        Random s = new Random();
        
        for(int i = 0 ; i< size; i++)
        {
            for(int j = 0 ; j < size  ; j++)
            {
                Figure figuraTemp = new Figure(s.nextInt(10),s.nextInt(10),s.nextInt(10),s.nextInt(10));
                matriz[i][j] = figuraTemp;
                if(j>0)
                {
                    matriz[i][j].setLeft(matriz[i][j-1].getRight());
                }
                if(i>0)
                {
                    matriz[i][j].setUp(matriz[i-1][j].getDown());
                }
            }
        }
        int nAzar = s.nextInt(size)+1;
        int i = 0;
        int x1,y1,x2,y2;
        
        while(i<nAzar)
        {
            x1 = s.nextInt(size);
            y1 = s.nextInt(size);
            x2 = s.nextInt(size);
            y2 = s.nextInt(size);
            Figure temp = matriz[x1][y1];
            matriz[x1][y1] = matriz[x2][y2];
            matriz[x2][y2] = temp;  
            i++;
        }
        this.ArrayFigure.clear();
        
        for(int x = 0; x<size; x++)
        {
            for(int y = 0; y<size; y++)
            {
                super.add(matriz[x][y]);
                matriz[x][y].setBounds((x)*100+500, (y)*100 +100, 100,100);
                this.ArrayFigure.add(matriz[x][y]);
            }
        }   
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        this.isPressedFigure(e.getPoint());
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        if(this.figuraMovida!=null)
        {
            this.ArrayFigure.remove(this.figuraMovida);
            //this.scanZone(e.getPoint());
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

    private void isPressedFigure(Point point)
    {
        for(Figure fig: this.ArrayFigure)
        {
            if(fig.pressed(point))
            {
                this.figuraMovida = fig;
            }
        }
    }

    private void scanZone(Point point) {
       
    }
}
